package com.zr.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.zr.dto.RedReceivingDM;

import net.sf.json.JSONObject;

public class ExportExcelAction implements ServletRequestAware{
	private HttpServletRequest req;
	private int rows;
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public  final  void exportExcel() {
		RedReceivingDM rec = new RedReceivingDM();
		List<RedReceivingDM> recs = new ArrayList<RedReceivingDM>();
		// 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("收货报表"); 
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        HSSFCell cell = row.createCell(0);  
        cell.setCellValue("统计");  
        cell.setCellStyle(style);
        cell = row.createCell(1);  
        cell.setCellValue("配送点");  
        cell.setCellStyle(style); 
        cell = row.createCell(2);  
        cell.setCellValue("报表时间");  
        cell.setCellStyle(style);  
        cell = row.createCell(3);  
        cell.setCellValue("报表生成时间");  
        cell.setCellStyle(style);  
        cell = row.createCell(4);  
        cell.setCellValue("发货配送点");  
        cell.setCellStyle(style);  
        cell = row.createCell(5);  
        cell.setCellValue("配送货物总重量(kg)");  
        cell.setCellStyle(style); 
        cell = row.createCell(6);  
        cell.setCellValue("总体积(m³)");  
        cell.setCellStyle(style); 
        cell = row.createCell(7);  
        cell.setCellValue("配送收入(元)");  
        cell.setCellStyle(style); 
        
    	int size = Integer.parseInt(req.getParameter("size"));
		int weight = Integer.parseInt(req.getParameter("weight"));
		int income = Integer.parseInt(req.getParameter("income"));
		String tid = req.getParameter("tid");
		String sname = req.getParameter("sname");
		String ptime = req.getParameter("ptime");
		String ctime = req.getParameter("ctime");
		String fname = req.getParameter("fname");
		
	    for (int i = 0; i < rows; i++) {
	    
			rec.setCtime(ctime);
			rec.setFname(fname);
			rec.setIncome(income);
			rec.setPtime(ptime);
			rec.setSize(size);
			rec.setSname(sname);
			rec.setWeight(weight);
			recs.add(rec);
	        
	        row.createCell(0).setCellValue(tid);  
	        row.createCell(1).setCellValue(sname);  
	        row.createCell(2).setCellValue(ptime);  
	        row.createCell(3).setCellValue(ctime);
	        row.createCell(4).setCellValue(fname);  
	        row.createCell(5).setCellValue(size);  
	        row.createCell(6).setCellValue(weight);
	        row.createCell(7).setCellValue(income);
	        
	    }
	        try {
				FileOutputStream fout = new FileOutputStream("D:/RedReceivingDM.xls");
				wb.write(fout); 
				fout.close(); 
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.req = request;
	}
	
}
